package com.test.mooan.labama.view_boom.info_input_boom;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.test.mooan.labama.constants_boom.Constant_boom.Data_Action_Phone;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.test.mooan.labama.R;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessData_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.view_boom.base_boom.BaseInfoInputViewManager_boom;
import com.test.mooan.labama.view_boom.base_boom.OnInputChangeListener_boom;
import com.test.mooan.labama.view_boom.widget_boom.BoomEditText;


public class InputViewManager_boom extends BaseInfoInputViewManager_boom {

    private TextView titleTv;
    private BoomEditText inputEt;

    private OnInputChangeListener_boom onInputChangeListener;

    public InputViewManager_boom(View view, ProcessData_boom data, String pageTag){
        super(view,data,pageTag);
    }

    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.view_input_tv_title);
        inputEt = mView.findViewById(R.id.view_input_et_input);

        setTitle(mData.gTitle());
        inputEt.setHint(Stringutil_boom.getSafeString(mData.gHint()));

        //联系人电话输入数字
        if (mData.gAction().equalsIgnoreCase(Data_Action_Phone)) inputEt.setInputType(InputType.TYPE_CLASS_NUMBER);

        //加纳卡号和卡名填充
        if (CountryInfo_boom.gInstance().isGhana() && mData.gParamName().equalsIgnoreCase("walletMobile")){
            inputEt.setText(UserInfoHelper_boom.gInstance().gMobile());
        }
        if (CountryInfo_boom.gInstance().isGhana()&& mData.gParamName().equalsIgnoreCase("walletUsername")){
            inputEt.setText(ProcessInfo_boom.getInstance().gRecipientName());
        }

        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (onInputChangeListener !=null) onInputChangeListener.onInputChange(s.toString());
            }
        });

        inputEt.stEventWord(pageTag +"_"+mData.gParamName()+"_INPUT");

//        inputEt.setOnFocusChangeListener((v, hasFocus) -> {
//            if (hasFocus) UserInfoHelper_boom.gInstance().addEventPoint((pageTag +"_"+mData.getParamName()+"_CLICK").toUpperCase());
//            else UserInfoHelper_boom.gInstance().addEventPoint((pageTag +"_"+mData.getParamName()+"_INPUT").toUpperCase(),getInput());
//        });

    }

    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputEt.getText());
    }

    @Override
    public String getInput() {
        return Stringutil_boom.getSafeString(inputEt.getText().toString());
    }

    public void addTextChangeListener(TextWatcher textWatcher){
        inputEt.addTextChangedListener(textWatcher);
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

    public void setOnInputChangeListener(OnInputChangeListener_boom onInputChangeListener) {
        this.onInputChangeListener = onInputChangeListener;
    }

    @Override
    public void clearInputFocus() {
        super.clearInputFocus();
        if (inputEt!=null) inputEt.clearFocus();
    }
}
